﻿using Oplug.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower
{
    public partial class FormCodePrint : Form
    {
        private DTO.SysConfig cfg;
        private Utilities Utility;
        public FormCodePrint()
        {
            InitializeComponent();
        }        

        private void FormCodePrint_Load(object sender, EventArgs e)
        {
            Utility = new Utilities();
            cfg = Utility.GetSysCfg();
        }

        private void textBoxPartNum_TextChanged(object sender, EventArgs e)
        {
            using (OplugLiveService Serv = new OplugLiveService(cfg.DBConn))
            {
                string[] arr = textBoxPartNum.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                string PartNumStr = string.Join("','", arr);
                PartNumStr = "'" + PartNumStr + "'";

                DataTable dt = Utility.SqlQuery(Serv.dbContext.Database, "Select PartNum,PartDescription,IUM from Part where PartNum in (" + PartNumStr + ")");
                if (dt.Rows.Count > 0)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    for(int i=0;i< dt.Rows.Count; i++)
                    {
                        stringBuilder.AppendLine(dt.Rows[i][1].ToString());
                    }
                    textBoxPartDesc.Text = stringBuilder.ToString();
                }
            }
        }

        private void ButtonPrint_Click(object sender, EventArgs e)
        {
            using (OplugLiveService Serv = new OplugLiveService(cfg.DBConn))
            {
                string[] arr = textBoxPartNum.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);                
                string PartNumStr = string.Join("','", arr);
                PartNumStr = "'" + PartNumStr + "'";

                DataTable dt = Utility.SqlQuery(Serv.dbContext.Database, "Select PartNum,PartDescription,IUM from Part where PartNum in (" + PartNumStr + ")");
                if (dt != null)
                {
                    if (checkBoxAuto.Checked)
                    {
                        using (PrintDoc printDoc = new PrintDoc())
                        {
                            printDoc.Run(Application.StartupPath + "/RDLC/ReportPartDesc.rdlc", dt);
                            //printDoc.Export("/RDLC/ReportPartDesc.rdlc", dt, "test.xls");
                        }
                    }
                    else
                    {
                        FormPrint form = new FormPrint("Oplug.Report.RDLC.ReportPartDesc.rdlc", dt, "", "ReportPartDesc");
                        form.Show();
                    }
                }
            }
                
            
        }

        private void textBoxPartNum_Leave(object sender, EventArgs e)
        {
            if (checkBoxAuto.Checked)
                ButtonPrint_Click(sender, e);
        }
    }
}
